package com.smarthome.BoxAndroidNew;

import com.smarthome.connect.TCPClient;

import android.app.AlertDialog;
import android.app.IntentService;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

public class ConnectService extends IntentService {
	private ProgressDialog pDialog;
	private String Login2 = "login2";

	public ConnectService() {
		super("ConnectService");
		// TODO Auto-generated constructor stub
	}

	public static TCPClient mTcpClient;

	String TAG1 = "Connectservice";
	String Tag2 = "MainActivity";
	String hostip =LocalData.IP_Anddress;
	
	int hostport = 11008;

	@Override
	protected void onHandleIntent(Intent intent) {
		try {
			Log.d(TAG1, "Services started-2");
			new connectTask().execute("");
		} catch (Exception e) {
			Log.e(TAG1, "faile to excute connecttask-2" + e);
		}
	}

	public class connectTask extends AsyncTask<String, String, TCPClient> {

		@Override
		protected TCPClient doInBackground(String... message) {
			Log.d(TAG1, "connectTask - in asycn task- 3");
			// we create a TCPClient object and
			mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
				@Override
				// here the messageReceived method is implemented
				public void messageReceived(String message) {
					// this method calls the onProgressUpdate
					Log.d(TAG1, "connectTask - in asycn task- 4");
					publishProgress(message);
				}
			});
			if (LocalData.isConnectsuccess == false) {
				try {

					Log.d(TAG1, "connectTask - in asycn task- 5");
					// mmTcpClient.run(LocalData.strIP,LocalData.strport);
					// mmTcpClient.run("172.16.10.37", 44444);
					mTcpClient.run(hostip, hostport);
					Log.d(TAG1, "Services started - in asycn task- 6");
				} catch (Exception e) {
					Log.e(TAG1, "" + e);
				}
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(final String... values) {
			super.onProgressUpdate(values);
			Log.e(TAG1, "Onprogressupdate" + values[0]);
			LocalData.strreceiver = values[0];
			//LocalData.TAG=values[1];
			Authen mOJResponsive = new Authen();
			mOJResponsive = check_tag(LocalData.strreceiver);
			messageRequest mmessage=new messageRequest();
			try {
			mmessage=JSonparse.getMess(LocalData.strreceiver);
//			if ((message.getDeviceId()!=null)&&(message.getValue()!=null)) 
				if (mmessage!=null)
				{
					 MyActivity.mainIns.OnRecievedData(mmessage);
					 //MyActivity.mainIns.mConnectedThread.write("1");
					 Log.d("nhancc","dv1"+mmessage.getDv1());
					 return;
				}
		    
				
			}
			catch(Exception ex) { 
				
			}
			
			// if (mOJResponsive.getTag()=="login")
		try {

				if (mOJResponsive.getTag().equals(LocalData.TAG_LOGIN)) {
					Log.d(Login2, "inside if");
					if (mOJResponsive.getSuccess() == true) {
						LocalData.islogin = true;
						Log.d(Login2, "login true");
					} else {
						LocalData.islogin = false;
						Log.d(Login2, "False");
					}
				}
				if (mOJResponsive.getTag().equals(LocalData.TAG_REGISTER)) {
					Log.d(LocalData.TAG_REGISTER, "inside if");
					if (mOJResponsive.getSuccess() == true) {
						LocalData.isregisted = true;
						Log.d(LocalData.TAG_REGISTER, "login true");
					} else {
						LocalData.isregisted = false;
						Log.d(LocalData.TAG_REGISTER, "False");
					}
					
				}
				else if (mOJResponsive.getTag().equals(LocalData.TAG_STATUS))
				{
					
					if (mOJResponsive.getSuccess() == true) {
						LocalData.isConnectsuccess = true;
						//Log.d(LocalData.TAG_REGISTER, "login true");
					} else {
						//LocalData.isregisted = false;
					//	Log.d(LocalData.TAG_REGISTER, "False");
					}
				}
					
			} catch (Exception e) {
				Log.e(LocalData.TAG_LOGIN, "" + e);
			}

			// in the arrayList we add the messaged received from server
			// notify the adapter that the data set has changed. This means that
			// new message received
			// from server was added to the list

		}
	}

	void alertmess(String mess) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(mess).setTitle("Thong Bao").setCancelable(false)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						return;
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}

	void alertfailmess(String mess) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(mess).setTitle("Thong Bao").setCancelable(false)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// finish();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}

	public Authen check_tag(String receiver) {
		Authen mOJResponsive = new Authen();
		mOJResponsive = JSonparse.getauthen(receiver);
		return mOJResponsive;
	}
}
